{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from get_features import getLbp\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading Data....\n"
     ]
    }
   ],
   "source": [
    "print 'Loading Data....'\n",
    "x_train = np.load('x_train.npy')\n",
    "x_test = np.load('x_test.npy')\n",
    "x_train = np.squeeze(x_train)\n",
    "# print len(x_train)\n",
    "# sys.exit()\n",
    "\n",
    "\n",
    "# Parameters\n",
    "learning_rate = 0.01\n",
    "training_epochs = 20\n",
    "\n",
    "\n",
    "# Network Parameters\n",
    "n_hidden_1 = 128\n",
    "n_hidden_2 = 50\n",
    "n_input = 256\n",
    "\n",
    "# tf Graph input\n",
    "X = tf.placeholder(\"float\", [None, n_input])\n",
    "\n",
    "weights = {\n",
    "    'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),\n",
    "    'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),\n",
    "    'decoder_h1': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])),\n",
    "    'decoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_input])),\n",
    "}\n",
    "biases = {\n",
    "    'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1])),\n",
    "    'encoder_b2': tf.Variable(tf.random_normal([n_hidden_2])),\n",
    "    'decoder_b1': tf.Variable(tf.random_normal([n_hidden_1])),\n",
    "    'decoder_b2': tf.Variable(tf.random_normal([n_input])),\n",
    "}\n",
    "\n",
    "# Building the encoder\n",
    "def encoder(x):\n",
    "    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']),\n",
    "                                   biases['encoder_b1']))\n",
    "\n",
    "    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']),\n",
    "                                   biases['encoder_b2']))\n",
    "    return layer_2\n",
    "\n",
    "\n",
    "# Building the decoder\n",
    "def decoder(x):\n",
    "    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']),\n",
    "                                   biases['decoder_b1']))\n",
    "\n",
    "    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']),\n",
    "                                   biases['decoder_b2']))\n",
    "    return layer_2\n",
    "\n",
    "# Construct model\n",
    "encoder_op = encoder(X)\n",
    "decoder_op = decoder(encoder_op)\n",
    "y_pred = decoder_op\n",
    "y_true = X\n",
    "\n",
    "cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))\n",
    "optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)\n",
    "\n",
    "init = tf.initialize_all_variables()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total Number of Batches: 47\n",
      "Epoch: 1 cost: 250.34\n",
      "Epoch: 2 cost: 250.082\n",
      "Epoch: 3 cost: 249.912\n",
      "Epoch: 4 cost: 249.808\n",
      "Epoch: 5 cost: 249.752\n",
      "Epoch: 6 cost: 249.74\n",
      "Epoch: 7 cost: 249.73\n",
      "Epoch: 8 cost: 249.729\n",
      "Epoch: 9 cost: 249.718\n",
      "Epoch: 10 cost: 249.717\n",
      "Epoch: 11 cost: 249.714\n",
      "Epoch: 12 cost: 249.713\n",
      "Epoch: 13 cost: 249.713\n",
      "Epoch: 14 cost: 249.711\n",
      "Epoch: 15 cost: 249.709\n",
      "Epoch: 16 cost: 249.708\n",
      "Epoch: 17 cost: 249.708\n",
      "Epoch: 18 cost: 249.7\n",
      "Epoch: 19 cost: 249.655\n",
      "Epoch: 20 cost: 249.654\n",
      "Optimization Finished!\n"
     ]
    }
   ],
   "source": [
    "Bsize = 256\n",
    "Tsize = len(x_train)\n",
    "Nbatch = int(Tsize/Bsize)\n",
    "if Tsize/(Bsize*1.0) != Nbatch:\n",
    "    Nbatch = Nbatch + 1\n",
    "print 'Total Number of Batches:',Nbatch\n",
    "nEpochs = 20\n",
    "\n",
    "# sess = tf.Session()\n",
    "\n",
    "#     _,c = sess.run([optimizer, cost],feed_dict={X: x_train[:256]})\n",
    "#     print c\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for epoch in range(nEpochs):\n",
    "        print \"Epoch:\",epoch+1,\n",
    "        for i in range(Nbatch):\n",
    "            if(i+1 == Nbatch):\n",
    "                a = (i)*Bsize+1\n",
    "                b = Tsize\n",
    "            else:\n",
    "                a = (i)*Bsize+1\n",
    "                b = (i+1)*Bsize\n",
    "\n",
    "    #             print a-1,b\n",
    "            batch_x = x_train[a-1:b]\n",
    "    #             print np.shape(x_train),np.shape(batch_x)\n",
    "            _,c = sess.run([optimizer, cost], feed_dict={X: batch_x})\n",
    "        print \"cost:\",c\n",
    "    print(\"Optimization Finished!\")\n",
    "    \n",
    "    w,b = sess.run([weights, biases])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "np.save('ae-weights.npy',w)\n",
    "np.save('ae-biases.npy',b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
